package com.cqw.threaddemo.thread;

/**
 * A thread state.  A thread can be in one of the following states:
 * <ul>
 * <li>{@link #NEW}<br>
 *     A thread that has not yet started is in this state.
 *     </li>
 * <li>{@link #RUNNABLE}<br>
 *     A thread executing in the Java virtual machine is in this state.
 *     </li>
 * <li>{@link #BLOCKED}<br>
 *     A thread that is blocked waiting for a monitor lock
 *     is in this state.
 *     </li>
 * <li>{@link #WAITING}<br>
 *     A thread that is waiting indefinitely for another thread to
 *     perform a particular action is in this state.
 *     </li>
 * <li>{@link #TIMED_WAITING}<br>
 *     A thread that is waiting for another thread to perform an action
 *     for up to a specified waiting time is in this state.
 *     </li>
 * <li>{@link #TERMINATED}<br>
 *     A thread that has exited is in this state.
 *     </li>
 * </ul>
 *
 * <p>
 * A thread can be in only one state at a given point in time.
 * These states are virtual machine states which do not reflect
 * any operating system thread states.
 *
 * @since   1.5
 * @see #getState
 */
/**
 * java线程的6中状态：
 *
 * new(新建),
 * runnable(运行状态),
 * blocked(阻塞 - 由synchronized锁导致进入该状态),
 * waiting(等待状态-不占用CPU，线程会让出锁，等待被其他线程唤醒进入BLOCKED 状态，重新竞争锁),
 * timed_waiting(超时等待 - 若为sleep进入该状态，线程不会释放锁，等到时间到了自动唤醒进入RUNNABLE状态
 *                         其他情况下线程会释放锁，等待其他线程唤醒，超时时间到了自动唤醒，然后进入 BLOCKED状态，重新竞争锁),
 * terminated(死亡状态)
 *
 *
 *
 * Java线程和操作系统线程的关系可参考该博客:https://blog.csdn.net/CringKong/article/details/79994511
 */
public class ThreadState {


    public static void main(String[] args) throws InterruptedException {
        //NEW
        Thread t1 = new Thread(()->{}, "t1");

        //RUNNABLE
        Thread t2 = new Thread(()->{
            while (true) {
            }
        });
        t2.start();

        //WAITING
        Thread t3 = new Thread(()->{
            synchronized (ThreadState.class){
                try {
                    t2.join();//该行代码的作用：t3线程等待t2线程执行结束后才能继续执行
                              //join()方法会释放锁,释放的是调用join()方法线程对象的锁，即会释放t2的锁，不会释放ThreadState.class对象的锁
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t3.start();

        //TIMED_WAITING
        Thread t4 = new Thread(()->{
            try {
                Thread.sleep(1000000);//交出cpu的控制权，在限定的时间内不去竞争cpu，并且不会释放锁
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t4.start();

        //BLOCKED
        Thread t5 = new Thread(()->{
            synchronized (ThreadState.class){}//t3中该锁未释放，故t5线程获取不到锁，一直处于阻塞状态
        });
        t5.start();

        //TERMINATED
        Thread t6 = new Thread(()->{});
        t6.start();




        Thread.sleep(1000);
        System.out.println(t1.getState());
        System.out.println(t2.getState());
        System.out.println(t3.getState());
        System.out.println(t4.getState());
        System.out.println(t5.getState());
        System.out.println(t6.getState());

    }
}